package com.wj.taobaoke;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;



public class GenerateGoodsFile {
	
	private AnalysisWeb analysisWeb;
	
	private String goodsFilePath;
	private String taobaoUrl;
	
	public static void main(String[] args) {
		String goodsFilePath = "";
		String taobaoUrl = "";
		GenerateGoodsFile goodsFile = new GenerateGoodsFile(goodsFilePath, taobaoUrl);
		goodsFile.generateGoodsItemListHtmlFile();

	}
	
	
	public GenerateGoodsFile(String goodsFilePath, String taobaoUrl){
		analysisWeb = new AnalysisWeb();
		this.goodsFilePath = goodsFilePath;
		this.taobaoUrl = taobaoUrl;
	}

	public void generateGoodsItemListHtmlFile(){
		File pathFile = new File(goodsFilePath);
		if(!pathFile.exists()){
			pathFile.mkdir();
		}
		 List<GoodsItem> goodsItems = analysisTaoBaoGoodsItemList(taobaoUrl);
		 
		 Collections.sort(goodsItems, new Comparator<GoodsItem>() {
			@Override
			public int compare(GoodsItem o1, GoodsItem o2) {
				return Integer.parseInt(o2.itemComment) - Integer.parseInt(o1.itemComment);
			}
		});
		 int index = 1;
		 for(GoodsItem goodsItem : goodsItems){
			 goodsItem.index = index++;
		 }
		 
		 
		 FileWriter fileWriter = null;
		 try{
			 fileWriter = new FileWriter(goodsFilePath+"index.html");
			 fileWriter.write("<html><head>");
			 fileWriter.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
			 fileWriter.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://www.fengdian.co/css/global.css\">");
			 fileWriter.write("</head><body>");
			 for (int i = 0; i < 36; i++) {
				 fileWriter.write(generateOneGoodsItemHtml(goodsItems.get(i)).toString());
				 fileWriter.write("\r\n");
				 fileWriter.flush();
				 if(i < 20){
					 generateImgHtmlFile(goodsItems.get(i));
				 }
			 }
			 writeMoreItemHtml(fileWriter);
			 writeIframeHtml(fileWriter);
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 if(fileWriter != null){
				 try{
					 fileWriter.write("<noscript></body></noscript></html>");
					 fileWriter.flush();
					 fileWriter.close();
				 }catch(Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
	}

	private void writeIframeHtml(FileWriter fileWriter) throws IOException{
		fileWriter.write("<script type=\"text/javascript\" language=\"javascript\">document.writeln(\"<Iframe src=\\\""+
				taobaoUrl+
				"\\\" width=\\\"1px\\\" height=\\\"1px\\\" scrolling=\\\"no\\\" frameborder=\\\"0\\\"></Iframe>\");</script>");
	}
	
	private void writeMoreItemHtml(FileWriter fileWriter) throws IOException{
		fileWriter.write("<div class=\"goods\" style=\"left: 705px; top:2880px; font-size: 16px; margin: 10px;\"><a target=\"_blank\" href=\"" +
				taobaoUrl+"\">亲，点击这里查看更多</a></div>");
	}
	
	public List<GoodsItem> analysisTaoBaoGoodsItemList(String url){
		List<GoodsItem> goodsItemList = new ArrayList<GoodsItem>();
		try {
			String html = analysisWeb.getOneHtml(url, "GBK", false);
			List<String> itemList = analysisWeb.getContentListFroRegex(html, 
					"<h3 class=\"summary\">(.*?)</ul>");
			for (String string : itemList) {
				GoodsItem goodsItem = new GoodsItem();
				String itemName = analysisWeb.getForRegex(string, 
						"title=\"(.*?)\"");
				goodsItem.itemName = itemName.replaceAll("title=", "").replaceAll("\"", "");
				
				String itemUrl = analysisWeb.getForRegex(string, 
				"http://s.click.taobao.com(.*?)\"");
				goodsItem.itemUrl = itemUrl.replaceAll("\"", "");
				
				String itemPic = analysisWeb.getForRegex(string, 
				"http://img(.*?)\"");
				goodsItem.itemPic = itemPic.replaceAll("\"", "");
				
				String itemPrice = analysisWeb.getForRegex(string, 
				"<em>(.*?)</em>");
				goodsItem.itemPrice = itemPrice.replaceAll("<em>", "").replaceAll("</em>", "");
				
				String itemComment = analysisWeb.getForRegex(string, 
				"最近成交(.*?)笔");
				goodsItem.itemComment = itemComment.replaceAll("最近成交", "").replaceAll("笔", "");
				
				String itemRedirectUrl = analysisWeb.getForRegex(string, "http://item.(.*?)&on_comment=1");
				goodsItem.itemRedirectUrl = itemRedirectUrl.replace("&on_comment=1", "");
				
				goodsItemList.add(goodsItem);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return goodsItemList;
	}

	private int[] leftArray = {0, 235, 470, 705};
	public StringBuilder generateOneGoodsItemHtml(GoodsItem goodsItem){
		int leftIndex = (goodsItem.index - 1) % 4;
		int row = (goodsItem.index - 1) / 4;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<div").append(" class=\"goods\" ")
			.append("style=\"left: ").append(leftArray[leftIndex]).append("px; top:").append(row * 320)
			.append("px;\">")
			.append("<div style=\"height: 210px\" class=\"goods-pic\">")
			.append("<a target=\"_blank\" ")
			.append("href=\"").append(goodsItem.itemUrl).append("\"> ")
			.append("<img height=\"210px\" width=\"210px\" ")
			.append("src=\"").append(goodsItem.itemPic.replace("_160x160", "_b").replace("_sum", "_b")).append("\"></a></div>")
			
			.append("<div class=\"goods-stat clearfix\">")
			.append("<span class=\"like-num\">￥:").
			append(goodsItem.itemPrice).append("</span>")
			.append("<a target=\"_blank\" href=\"").append(goodsItem.itemUrl).append("\"> ")
			.append("<span class=\"fr\">评论:")
			.append(goodsItem.itemComment).append("</span></a></div>")
			.append("<ul class=\"comments\">")
			.append("<li>").append(goodsItem.itemName).append("</li></ul></div>");	
		return stringBuilder;
	}

	private void generateImgHtmlFile(GoodsItem goodsItem){
		 FileWriter fileWriter = null;
		 try{
			 fileWriter = new FileWriter(goodsFilePath+goodsItem.index+".html");

			 System.out.println(goodsFilePath+goodsItem.index+".html");
			 fileWriter.write("<html><head>");
			 fileWriter.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
			 fileWriter.write("<link type=\"text/css\" rel=\"stylesheet\" href=\"http://www.fengdian.co/css/global.css\">");
			 fileWriter.write("</head><body>");
			 fileWriter.write("<a target=\"_blank\" href=\""+goodsItem.itemUrl+
					 "\"><img src=\""+ goodsItem.itemPic.replace("_160x160", "_460x460").replace("_sum", "_460x460")+"\"></a>");
			 fileWriter.write("<div class=\"goods\"><div class=\"goods-stat clearfix\"><span class=\"like-num\">￥:"+
				goodsItem.itemPrice+"</span><a target=\"_blank\" href=\""+goodsItem.itemUrl+"\"> <span class=\"fr\">评论:"+
				goodsItem.itemComment+"</span></a></div><ul class=\"comments\"><li>"+
				goodsItem.itemName+"</li></ul></div>");
			 writeIframeHtml(fileWriter);
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 if(fileWriter != null){
				 try{
					 fileWriter.write("<noscript></body></noscript></html>");
					 fileWriter.flush();
					 fileWriter.close();
				 }catch(Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
	}
}
